﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TSV.Models;
using System.Data.Common;
using TSV.Library.Common;
namespace TSV.Areas.Administration.Models
{
    public class OwnerStudentCardDao : BaseDao
    {
        #region "Get"
        public List<sp_GetOwnerStudentCardResult> GetList(string keyword,string fromDate,string toDate)
        {
            return dbContext.sp_GetOwnerStudentCard(keyword, fromDate, toDate).ToList();

        }
        public List<sp_GetOwnerStudentCardResult> GetList()
        {
            return GetList("","","");
        }
        public List<sp_GetOwnerStudentCardResult> GetList(string keyword)
        {
            return GetList(keyword, "", "");
        }
        public OwnerStudentCard GetById(string id)
        {
            return dbContext.OwnerStudentCards.Where(c => c.ID.ToString().Equals(id)).SingleOrDefault<OwnerStudentCard>();
        }
        public List<sp_GetOwnerStudentCardResult> Sort(List<sp_GetOwnerStudentCardResult> list, string sortColumn, string sortOrder)
        {
            int order;

            if (sortOrder == "desc")
            {
                order = -1;
            }
            else
            {
                order = 1;
            }
            switch (sortColumn)
            {

                case "FullName":
                    list.Sort(
                         delegate(sp_GetOwnerStudentCardResult m1, sp_GetOwnerStudentCardResult m2)
                         { return m1.FullName.CompareTo(m2.FullName) * order; });
                    break;
                case "ContactAddress":
                    list.Sort(
                         delegate(sp_GetOwnerStudentCardResult m1, sp_GetOwnerStudentCardResult m2)
                         { return m1.ContactAddress.CompareTo(m2.ContactAddress) * order; });
                    break;
                case "Email":
                    list.Sort(
                         delegate(sp_GetOwnerStudentCardResult m1, sp_GetOwnerStudentCardResult m2)
                         { return m1.Email.CompareTo(m2.Email) * order; });
                    break;
                case "Phone":
                    list.Sort(
                         delegate(sp_GetOwnerStudentCardResult m1, sp_GetOwnerStudentCardResult m2)
                         { return m1.Phone.CompareTo(m2.Phone) * order; });
                    break;
                case "School":
                    list.Sort(
                         delegate(sp_GetOwnerStudentCardResult m1, sp_GetOwnerStudentCardResult m2)
                         { return m1.School.CompareTo(m2.School) * order; });
                    break;
               
            }

            return list;
        }
        #endregion
        #region edit"
        public Message DeleteList(string ids)
        {
            Message msg = null;
            DbTransaction trans = null;
            bool canDelete = true;
            try
            {
                dbContext.Connection.Open();
                trans = dbContext.Connection.BeginTransaction();
                dbContext.Transaction = trans;

                if (!string.IsNullOrEmpty(ids))
                {
                    ids = ids.TrimEnd(',');
                    string[] idArr = ids.Split(',');
                    foreach (string id in idArr)
                    {

                        OwnerStudentCard oscObj = GetById(id);
                        if (oscObj != null)
                        {
                            dbContext.OwnerStudentCards.DeleteOnSubmit(oscObj);
                            dbContext.SubmitChanges();

                        }
                    }
                    if (canDelete)
                    {
                        // Show succes message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, idArr.Count().ToString() + " Record(s)", "deleted");
                        trans.Commit();
                    }
                    else
                    {
                        trans.Rollback();
                    }
                }
            }
            catch (Exception)
            {
                if (trans != null) trans.Rollback();
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }
        public Message Update(OwnerStudentCard objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    OwnerStudentCard objDb = GetById(objUI.ID.ToString());

                    if (objDb != null)
                    {
                        // Update info by objUI
                        objDb.FullName = objUI.FullName;
                        objDb.Gender = objUI.Gender;
                        objDb.ContactAddress = objUI.ContactAddress;
                        objDb.ReceiveAdrress = objUI.ReceiveAdrress;
                        objDb.Phone = objUI.Phone;
                        objDb.Email = objUI.Email;
                        objDb.School = objUI.School;
                        objDb.SchoolYear = objUI.SchoolYear;

                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Người đăng ký thẻ '" + objUI.FullName + "'", "cập nhật");

                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }
        public Message Insert(OwnerStudentCard objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Set more info                    
                    objUI.CreateDate = DateTime.Now;

                    dbContext.OwnerStudentCards.InsertOnSubmit(objUI);
                    dbContext.SubmitChanges();

                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Đăng ký sở hữu thẻ", "");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }
        #endregion
    }
}